package com.gskj.gulimall.auth.controller;

/**
 * @ClassName Auth2Controller
 * @Description TODO
 * @Author 康纳
 * @Date 2021-03-17
 */

import club.gskj.common.utils.HttpUtils;
import club.gskj.common.utils.R;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gskj.gulimall.auth.feign.MemberFeignService;
import club.gskj.common.vo.MemberRespVo;
import com.gskj.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 社交登陆
 * Auth2.0 微博登陆
 */
@Slf4j
@Controller
public class Auth2Controller {

    @Autowired
    private MemberFeignService memberFeignService;

    /**
     * 微博登陆，授权成功 回调url
     *
     * @param code 微博授权码
     */
    @GetMapping("/auth2.0/weibo/success")
    public String successWeiBo(@RequestParam("code") String code, HttpSession session) throws Exception {

        // 通过 code 获取 微博accessToken
        Map<String, String> param = new HashMap<>(5);

        param.put("client_id", "898547533");
        param.put("client_secret", "23096abfacf21b5599051ef83520780f");
        param.put("grant_type", "authorization_code");
        param.put("redirect_uri", "http://auth.gulimall.com/auth2.0/weibo/success");
        param.put("code", code);

        HttpResponse response = null;
        // 发送请求获取 access_token
        response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "POST", null, null, param);
        if (response.getStatusLine().getStatusCode() == 200) {
            // 响应 200，获取access_token成功
            String json = EntityUtils.toString(response.getEntity());
            System.out.println("json " + json);
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);

            // 调用member-service 查询该社交用户是否已存在 系统中
            // 如果为第一次登陆，为当前社交会员信息账号，以后社交账号就对应该会员账号
            R r = memberFeignService.authLogin(socialUser);

            if (r.getCode() == 0) {
                // 获取登陆用户信息
                MemberRespVo data = r.getData("data", new TypeReference<MemberRespVo>() {
                });

                log.info("登陆成功 :{}", data.toString());

                // 将用户信息存放到 session
                // TODO 1. 默认发放的令牌： session=xxxxx, 作用域为当前域名。解决子域无法使用
                // TODO 2. session存储的数据默认使用JDK的序列化， 修改为JSON序列化方法。

                session.setAttribute("loginUser", data);
                // 登陆成功后，跳转到首页
                return "redirect:http://gulimall.com/";
            } else {
                // 登陆失败，返回登陆页面
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }
        // 登陆失败，返回登陆页面
        return "redirect:http://auth.gulimall.com/login.html";
    }
}
